﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="OrderItemsCtrl.ascx.cs" Inherits="WebUI.Controls.OrderItemsCtrl" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<script type="text/javascript">
    var <%=ClientID %>clickOnCheckbox;
    var <%=ClientID %>selectedRow = null;       
    var <%=ClientID %>selectedCheckbox = null; 
    var <%=ClientID %>selectedID = null; 
    var <%=ClientID %>selectedClass = null; 
    
    function resetGridSelection<%=ClientID %>(){
        try{
            <%=ClientID %>selectedRow = null; 
            if(<%=ClientID %>selectedCheckbox != null)
                <%=ClientID %>selectedCheckbox.checked = false;      
            <%=ClientID %>selectedCheckbox = null; 
            <%=ClientID %>selectedID = null; 
            <%=ClientID %>selectedClass = null; 
        }
        catch(ex){
        }
    }
    
    function clickOnCheckBox<%=ClientID %>(cb){
        <%=ClientID %>clickOnCheckbox = true;
    }
    
    function onItemClick<%=ClientID %>(sender)
    {
        var hidSelectedID = document.getElementById("<%= hidSelectedID.ClientID %>");
        if(<%=ClientID %>clickOnCheckbox){
            <%=ClientID %>clickOnCheckbox = false;
            return;
        }

        if(<%=ClientID %>selectedRow == null){
            <%=ClientID %>selectedRow = document.getElementById("<%=ClientID %>" + "_row_" + hidSelectedID.value);
        }
        
        if(<%=ClientID %>selectedRow != null){
            if(<%=ClientID %>selectedRow.className)
                <%=ClientID %>selectedRow.className = <%=ClientID %>selectedRow.getAttribute("orgclass");
            else
                <%=ClientID %>selectedRow.setAttribute("class", <%=ClientID %>selectedClass);
            if(<%=ClientID %>selectedCheckbox != null)
                <%=ClientID %>selectedCheckbox.checked = false;
            else{
                var chks = <%=ClientID %>selectedRow.getElementsByTagName("input");
                 for(var i = 0; i < chks.length; i++){
                    if(chks[i].type == "checkbox" && chks[i].id.indexOf("cbSelect") >= 0){
                        chks[i].checked = false;
                        break;
                    }
                }
            }
        }
        
        if(sender.className){
            sender.className = "CustomGridSelectedItem";
        }
        else{
            <%=ClientID %>selectedClass = sender.getAttribute("class");
            sender.setAttribute("class", "CustomGridSelectedItem");
        }
        <%=ClientID %>selectedRow = sender;
        
        var inputs = sender.getElementsByTagName("input");
        for(var i = 0; i < inputs.length; i++){
            if(inputs[i].type == "checkbox" && inputs[i].id.indexOf("cbSelect") >= 0){
                <%=ClientID %>selectedCheckbox = inputs[i];
                inputs[i].checked = true;
                break;
            }
        }

        hidSelectedID.value = <%=ClientID %>selectedRow.getAttribute("ItemID");
    }
    
    function selectAll<%=ClientID %>(cbAll){
        var grid = document.getElementById("<%=grid.ClientID %>");
        var inputs = grid.getElementsByTagName("input");
        for(var i = 0; i < inputs.length; i++){
            if(inputs[i].type == "checkbox"){
                inputs[i].checked = cbAll.checked;
            }
        }
    }
    
    function getSelectedItemID<%=ClientID %>(){
        if(document.getElementById("<%= hidSelectedID.ClientID %>").value == "")
            return null;
        return document.getElementById("<%= hidSelectedID.ClientID %>").value;
    }

    function updateQtyChange<%=ClientID %>(txt, id){
        var btn = document.getElementById("<%= btnChangeQty.ClientID %>");
        var hidID = document.getElementById("<%= hidID.ClientID %>");
        var hidQty = document.getElementById("<%= hidQty.ClientID %>");

        hidID.value = id;
        hidQty.value = txt.value;
        btn.click();
    }

    function deleteProductByID<%=ClientID %>(id){
        var ok = confirm("Are you sure to delete this item?");
        if(!ok)
            return;
        var hidID = document.getElementById("<%= hidID.ClientID %>");
        hidID.value = id;
        var btn = document.getElementById("<%= btnDelete.ClientID %>");
        btn.click();
    }

    function getTotalAmt<%=ClientID %>(){
        var hidTotalAmt  = document.getElementById("<%=hidTotalAmt.ClientID %>");
        return hidTotalAmt.value;
    }

    function showGrid(visible){
        var grid = document.getElementById("divGrid");
        if(visible)
            grid.style.display = "block";
        else
            grid.style.display = "none";
    }

    function HasCheckedItems<%=ClientID %>(){
        var grid = document.getElementById("<%=grid.ClientID %>");
        var inputs = grid.getElementsByTagName("input");
        var ok = false;
        for(var i = 0; i < inputs.length; i++){
            if(inputs[i].type == "checkbox"  && inputs[i].id.indexOf("cbSelect") >= 0 && inputs[i].checked == true ){
                ok = true;
            }
        }
        return ok;
    }

    function openBC(i){
        window.open('<%= POSSystem.BLL.Utilities.GetConfigAppSetting("BusinessCardUrl") %>' + i,  '', 'width=600,height=600,resizable=yes');
    }
</script>
<div id='divGrid'>
<asp:DataGrid ID="grid" runat="server" AllowPaging="true" AllowSorting="true" GridLines="None" CssClass="customGrid"
    AutoGenerateColumns="false" EmptyDataText="No record to display" ItemStyle-Wrap="true" CellPadding="1" CellSpacing="1"
    OnSortCommand="grid_OnSortCommand" OnItemCreated="grid_OnItemCreated" OnItemDataBound="grid_OnItemDataBound"
    OnPageIndexChanged="grid_OnPageIndexChanged" AllowCustomPaging="true" UseAccessibleHeader="true" DataKeyField="ID">
    <PagerStyle Mode="NumericPages" PageButtonCount="10" />
    <Columns>
       <asp:TemplateColumn ItemStyle-Width="10px" HeaderStyle-Width="10px" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
            <HeaderTemplate><asp:CheckBox ID="cbSelectAll" runat="server" /></HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="cbSelect" runat="server" ItemID='<%# Eval("ID") %>' />
            </ItemTemplate>
        </asp:TemplateColumn>
        <%--start modify here--%>
        <asp:BoundColumn ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="150px" DataField="ID" SortExpression="ID" HeaderText="ID" Visible="false"></asp:BoundColumn>
        <%--<asp:BoundColumn ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="250px" DataField="ProductName" HeaderText="Product"></asp:BoundColumn>--%>
        <asp:TemplateColumn ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="250px"  HeaderText="Product">
            <ItemTemplate>
                <a style='cursor:pointer;' href='<%# POSSystem.BLL.Utilities.GetProductImageUrl(ResolveUrl(Eval("OriginalUrl").ToString())) %>' onclick='<%# (int)Eval("Category") == 2 ? "openBC(\"" + Eval("ProductID") + "\"); return false;" : "" %>' rel='<%# (int)Eval("Category") == 2 ? "businesscard" : "lightbox[roadtrip]" %>'>
                    <asp:Image style='border: 0px none white; height:24px' ImageUrl='<%#  (int)Eval("Category") == 2 ? "~/Images/cardcopy.png" : POSSystem.BLL.Utilities.GetProductImageUrl(Eval("ThumbUrl"))  %>' runat="server" />
                </a>
                <asp:Label ID="lblProductName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateColumn>
        
        <%--<asp:BoundColumn ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="80px" DataField="Quantity"  HeaderText="Quantity"></asp:BoundColumn>--%>
        <asp:TemplateColumn ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="50px" HeaderText="Quantity">
            <ItemTemplate>
                <asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:BoundColumn ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="80px" DataField="CustomerPrice"  HeaderText="Price"></asp:BoundColumn>
        <asp:BoundColumn ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="80px" DataField="GST"  HeaderText="GST"></asp:BoundColumn>
        <asp:BoundColumn ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="80px" DataField="TotalAmount"  HeaderText="Total"></asp:BoundColumn>
        <asp:TemplateColumn ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="20px" HeaderText="">
            <ItemTemplate>
                <asp:LinkButton ID="btnDelete" runat="server" ToolTip="Delete" OnClientClick='<%# string.Format("deleteProductByID{1}({0}); return false;", Eval("ProductID"), ClientID) %>'>
                    <asp:Image ImageUrl="~/Images/delete_icon.gif" runat="server" ID="imgDelete" />
                </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateColumn>
        <%--end modify here--%>
    </Columns>
</asp:DataGrid>
</div>
<asp:HiddenField ID="hidQty" runat="server" />
<asp:HiddenField ID="hidID" runat="server" />
<asp:HiddenField ID="hidTotalAmt" runat="server" />
<asp:Button style="display:none" ID="btnChangeQty" runat="server" OnClick="btnChangeQty_OnClick" />
<asp:Button style="display:none" ID="btnDelete" runat="server" OnClick="btnDelete_OnClick" />
<asp:HiddenField ID="hidSelectedID" runat="server" />